package com.thinkit.bigdata.web.controller.system;

import com.thinkit.bigdata.core.feature.orm.mybatis.Page;
import com.thinkit.bigdata.core.util.StringUtil;
import com.thinkit.bigdata.web.model.result.ConfigResult;
import com.thinkit.bigdata.web.model.result.Constants;
import com.thinkit.bigdata.web.model.sec.User;
import com.thinkit.bigdata.web.model.sys.SysConfig;
import com.thinkit.bigdata.web.model.sys.SysConfigExample;
import com.thinkit.bigdata.web.service.sys.SysConfigService;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.apache.shiro.SecurityUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * Created by lihongli on 2018/9/4
 */
@Controller
@RequestMapping("sysConfig")
public class SysConfigController {

    private static Logger logger = Logger.getLogger(SysConfigController.class);

    @Resource
    private SysConfigService sysConfigService;

    /**
     * 跳转到系统配置页面
     *
     * @param model
     * @return
     */
    @RequestMapping("/sysConfigs")
    public String sysConfigs(Model model) {
        logger.info("请求系统配置页面");
        return "ConfigViews/sysConfig";
    }

    /**
     * 分页查询配置信息
     *
     * @param request
     * @param pageNum
     * @param pageSize
     * @return
     */
    @ResponseBody
    @RequestMapping("/sysConfigByPage")
    public Page<SysConfig> sysConfigByPage(
            HttpServletRequest request,
            @RequestParam(value = "sortOrder") String sortOrder,
            @RequestParam(value = "sortName") String sortName,
            @RequestParam(value = "pageNum", defaultValue = "1") int pageNum,
            @RequestParam(value = "pageSize", defaultValue = "10") int pageSize) {
        // 查询参数 配置编码、配置名称
        String code = request.getParameter("code");
        String name = request.getParameter("name");
        String logTxt = "【系统配置分页查询】";
        logger.info(logTxt + "查询条件: pageNum=" + pageNum);
        logger.info(logTxt + "查询条件: pageSize=" + pageSize);
        logger.info(logTxt + "查询条件: code=" + code);
        logger.info(logTxt + "查询条件: name=" + name);
        // 查询条件设置
        SysConfigExample example = new SysConfigExample();
        SysConfigExample.Criteria criteria = example.createCriteria();
        if (StringUtil.isNotEmpty(code)) {
            criteria.andCodeLike("%" + code + "%");
        }
        if (StringUtil.isNotEmpty(name)) {
            criteria.andNameLike("%" + name + "%");
        }
        // 排序方式设置
        example.setOrderByClause(sortName + " " + sortOrder);
        // 分页信息设置
        Page<SysConfig> page = new Page<>(pageNum, pageSize);
        sysConfigService.selectByExample(page, example);
        logger.info(logTxt + "查询完成，共计查询到" + page.getResult().size() + "条系统配置信息");
        return page;
    }

    /**
     * @param request
     * @param code
     * @return
     */
    @ResponseBody
    @RequestMapping("/codeUniqueCheck")
    public ConfigResult codeUniqueCheck(HttpServletRequest request, String code) {
        logger.info("进入系统配置code唯一性校验方法code为" + code);
        SysConfigExample example = new SysConfigExample();
        example.createCriteria().andCodeEqualTo(code);
        int count = sysConfigService.countByExample(example);
        if (count == 0) {
            logger.info(code + "不存在，唯一性检查通过");
            return new ConfigResult(Constants.SUCCESS, "√");
        } else {
            logger.info(code + "已存在，请修改");
            return new ConfigResult(Constants.ERROR, "该编码已存在，请修改");
        }
    }

    /**
     * 添加系统配置信息
     *
     * @param sysConfig
     * @return
     */
    @ResponseBody
    @RequestMapping("/sysConfigAdd")
    public ConfigResult sysConfigAdd(HttpServletRequest request, SysConfig sysConfig) {
        String logTxt = "【系统配置新增】";
        logger.info(logTxt + sysConfig.toString());
        // 获取当前登录用户信息
        User userInfo = (User) SecurityUtils.getSubject().getSession().getAttribute(Constants.SESSION_USER_INFO);
        // 设置修改人信息
        sysConfig.setLastupdateuserid(userInfo.getId());
        sysConfig.setLastupdateusername(userInfo.getUsername());
        sysConfig.setLastupdatetime(new Date());
        // 添加
        try {
            int rowNum = sysConfigService.insert(sysConfig);
            if (rowNum > 0) {
                logger.info(logTxt + "数据库成功插入" + rowNum + "条系统配置信息");
                return new ConfigResult(Constants.SUCCESS, "系统配置信息添加成功");
            } else {
                logger.info(logTxt + "系统配置信息添加失败");
                return new ConfigResult(Constants.SUCCESS, "系统配置信息添加成功");
            }
        } catch (Exception e) {
            logger.error(logTxt + "系统配置添加错误", e);
            return new ConfigResult(Constants.ERROR, "系统配置添加错误");
        }
    }

    /**
     * 根据主键获取系统配置信息
     *
     * @param code
     * @return
     */
    @ResponseBody
    @RequestMapping("/sysConfigGetByPK")
    public SysConfig sysConfigGetByPK(HttpServletRequest request, String code) {
        logger.info("获取code为【" + code + "】的系统配置信息");
        return sysConfigService.sysConfigGetByPK(code);
    }

    /**
     * 修改系统配置信息
     *
     * @param request
     * @param sysConfig
     * @return
     */
    @ResponseBody
    @RequestMapping("/sysConfigUpdate")
    public ConfigResult sysConfigUpdate(HttpServletRequest request, SysConfig sysConfig) {
        String logTxt = "【系统配置修改】";
        logger.info(logTxt + sysConfig.toString());
        // 获取登录者信息 并设置修改信息
        User userInfo = (User) request.getSession().getAttribute("userInfo");
        sysConfig.setLastupdateusername(userInfo.getUsername());
        sysConfig.setLastupdatetime(new Date());
        try {
            int rowNum = sysConfigService.update(sysConfig);
            if (rowNum > 0) {
                logger.info(logTxt + "数据库成功修改" + rowNum + "条信息");
                return new ConfigResult(Constants.SUCCESS, "系统配置信息修改成功");
            } else {
                logger.info(logTxt + "系统配置信息修改失败");
                return new ConfigResult(Constants.FAIL, "系统配置信息修改失败");
            }
        } catch (Exception e) {
            logger.error(logTxt + "系统配置信息修改错误", e);
            return new ConfigResult(Constants.ERROR, "系统配置信息修改错误");
        }
    }

    /**
     * 根据code删除对应的sysConfig
     *
     * @param request
     * @param code
     * @return
     */
    /*@ResponseBody
    @RequestMapping("/sysConfigDelete")
    public ConfigResult sysConfigDelete(HttpServletRequest request, String code) {
        String logTxt = "【删除系统配置】";
        logger.info(logTxt + "删除code为" + code + "的系统配置信息");
        try {
            int rowNum = sysConfigService.delete(code);
            if (rowNum > 0) {
                logger.info(logTxt + "数据库成功删除" + rowNum + "条数据");
                return new ConfigResult(Constants.SUCCESS, "系统配置信息删除成功");
            } else {
                logger.info(logTxt + "系统配置信息删除失败");
                return new ConfigResult(Constants.FAIL, "系统配置信息删除失败");
            }
        } catch (Exception e) {
            logger.error(logTxt + "系统配置信息删除错误", e);
            return new ConfigResult(Constants.ERROR, "系统配置信息删除错误");
        }
    }*/

    /**
     * 根据code删除对应的sysConfig
     *
     * @param request
     * @param codeList
     * @return
     */
    @ResponseBody
    @RequestMapping("/sysConfigDelete")
    public ConfigResult sysConfigDelete(HttpServletRequest request, @RequestParam("codeList") List<String> codeList) {
        String logTxt = "【删除系统配置】";
        if(codeList.isEmpty()){
            logger.info(logTxt + "删除code为为空");
            return new ConfigResult(Constants.WARNING, "没有选中要删除的配置信息");
        }
        logger.info(logTxt + "删除code为" + StringUtils.join(codeList, ',') + "的系统配置信息");
        try {
            int rowNum = sysConfigService.delete(codeList);
            if (rowNum > 0) {
                logger.info(logTxt + "数据库成功删除" + rowNum + "条数据");
                return new ConfigResult(Constants.SUCCESS, "系统配置信息删除成功");
            } else {
                logger.info(logTxt + "系统配置信息删除失败");
                return new ConfigResult(Constants.FAIL, "系统配置信息删除失败");
            }
        } catch (Exception e) {
            logger.error(logTxt + "系统配置信息删除错误", e);
            return new ConfigResult(Constants.ERROR, "系统配置信息删除错误");
        }
    }
}
